//
//  JHPersonalViewController.m
//  Liberty Voice
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "JHPersonalViewController.h"
#import "JHPersonTableViewCell.h"
#import "dengluViewController.h"
#import "JHPersonal.h"
#import "JHEliminateViewController.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.h"
#import "JHCollectViewController.h"
#import "JHPlayBackViewController.h"

#import <ShareSDK/ShareSDK.h>

#import "WeiboSDK.h"

static CGFloat kImageOriginHight = 230;
static CGFloat kTempHeight = 60.0f;

@interface JHPersonalViewController ()<UITableViewDataSource,UITableViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *perarr;
@property (nonatomic, assign) BOOL b;

@end

@implementation JHPersonalViewController
- (void)dealloc
{
    [_image1 release];
    [_image2 release];
    [_button1 release];
    [_tableView release];
    [_label1 release];
    [_perarr release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我";
    self.arr = @[@"Collect", @"Record ", @"Clearn", @"Suggest", @"About"];

    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.contentInset = UIEdgeInsetsMake(kImageOriginHight, 0, 0, 0);
    [self.view addSubview:self.tableView];
    self.image2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"98a5dd489b92abef8edae6e733b66ce8"]];
    self.image2.frame = CGRectMake(0, -kImageOriginHight - kTempHeight, self.tableView.frame.size.width, kImageOriginHight + kTempHeight);
    _tableView.separatorColor = [UIColor whiteColor];

    [self.tableView addSubview:self.image2];
    [_tableView release];
    [_image2 release];
    
    
    self.image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"788d0f720f450544ae54f910e0e1f3b5-2"]];
    self.image1.userInteractionEnabled = NO;
    UIGestureRecognizer *gesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.image1 addGestureRecognizer:gesture1];
    _image1.frame = CGRectMake((_image2.frame.size.width - 80) / 2, _image2.frame.origin.y / 2 - 50, 80, 80);
    _image1.contentMode = UIViewContentModeScaleAspectFit;
    
    self.image1.layer.cornerRadius = 40;
    _image1.layer.masksToBounds = YES;
    [self.tableView addSubview:_image1];
    [self.view bringSubviewToFront:_image1];
    [_image1 release];
    
    
    self.button1 = [[UIButton alloc] initWithFrame:CGRectMake((_image2.frame.size.width - 80) / 3.1, _image2.frame.origin.y / 2 - 50 + 80, self.view.frame.size.width / 2, 40)];
//    _button1.backgroundColor = [UIColor orangeColor];
    [_button1 setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    _button1.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    [_button1 setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
    [_button1 setTitle:@"登录" forState:UIControlStateNormal];
    [self.tableView addSubview:_button1];
    [self.view bringSubviewToFront:_button1];
    [_button1 addTarget:self action:@selector(dengru:) forControlEvents:UIControlEventTouchUpInside];
    [_button1 release];
    
    UIButton *buttonzhuxiao = [[UIButton alloc] initWithFrame:CGRectMake((_image2.frame.size.width - 65), _image2.frame.origin.y / 1.25, self.view.frame.size.width / 5, 40)];
    buttonzhuxiao.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    [buttonzhuxiao setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
    [buttonzhuxiao setTitle:@"注销" forState:UIControlStateNormal];
//    buttonzhuxiao.backgroundColor = [UIColor greenColor];
    [self.tableView addSubview:buttonzhuxiao];
//    buttonzhuxiao.backgroundColor = [UIColor orangeColor];
    [buttonzhuxiao addTarget:self action:@selector(zhuxiao) forControlEvents:UIControlEventTouchUpInside];
    

    
    [self gerData];
}

- (void)clear
{
    NSInteger size = [[SDImageCache sharedImageCache] getSize];
    float flSize = size /1024.0 /1024.0;
    NSString *str = [NSString stringWithFormat:@"%.2f.M",flSize];
    
    
    UIAlertView *aview = [[UIAlertView alloc] initWithTitle:@"亲,您有缓存哦" message:str delegate:self cancelButtonTitle:nil
    otherButtonTitles:@"清理", nil];
    [aview show];
    //清除磁盘缓存
    [[SDImageCache sharedImageCache] clearDisk];
    //清除内存缓存
    //[[[SDWebImageManager sharedManager] imageCache] clearMemory];
    
    //清除系统缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    [aview release];
}

- (void)gerData
{
    JHPersonal *p1 = [[JHPersonal alloc] initWithpicStr:@"043EFE1F-D2A3-4E95-BD4C-2668DD00E880"];
    JHPersonal *p2 = [[JHPersonal alloc] initWithpicStr:@"D0BC98B6-7B6B-4B80-8D54-22A99E051BEA"];
    JHPersonal *p3 = [[JHPersonal alloc] initWithpicStr:@"834187E3-07A8-411F-AD8F-D02053B41741"];
    JHPersonal *p4 = [[JHPersonal alloc] initWithpicStr:@"9B4F8E32-4FCD-4D17-8B4E-C3D700A56D2D"];
    JHPersonal *p5 = [[JHPersonal alloc] initWithpicStr:@"CC18CEE7-349E-4164-935C-0CC36316C209"];
    self.perarr = [@[p1, p2, p3, p4, p5]mutableCopy];
}

-(void)tapAction:(UITapGestureRecognizer *)sender
{
    // 初始化UIImagePickerController 类 获取到UIImagePickerController对象
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    // 设置类型为照片类
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 指定代理人
    imagePicker.delegate = self;
    // 允许编辑
    imagePicker.allowsEditing = YES;
    // 模态调用
    [self presentViewController:imagePicker animated:YES completion:^{
        
    }];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.image1.image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
    // 通过模态返回
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

// 3.最后选中结束后 通过模态消失
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)dengru:(UIButton *)btn
{
    ShareType type = 0;
    
    type = ShareTypeSinaWeibo;
    
    [ShareSDK getUserInfoWithType:type authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
        if (result) {
            NSLog(@"授权登陆成功，已获取用户信息");
            NSString *uid = [userInfo uid];
            NSString *nickname = [userInfo nickname];
            NSString *profileImage = [userInfo profileImage];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Code4App" message:[NSString stringWithFormat:@"授权登陆成功,用户ID:%@,昵称:%@,头像:%@",uid,nickname,profileImage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            NSLog(@"source:%@",[userInfo sourceData]);
            NSLog(@"uid:%@",[userInfo uid]);
            
            [_image1 sd_setImageWithURL:[NSURL URLWithString:@"http://tp4.sinaimg.cn/2191506675/50/0/1"] placeholderImage:[UIImage imageNamed:@"/Users/dlios/Desktop/e8a11791c1320c2966f704382e08e7ec.jpg"]];
            [_button1 setTitle:nickname forState:UIControlStateNormal];
            
        }else{
            NSLog(@"分享失败,错误码:%ld,错误描述%@",(long)[error errorCode],[error errorDescription]);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Code4App" message:@"授权失败，请重新登录" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    }];

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 9;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    JHPersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[JHPersonTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse]autorelease];
    }
    JHPersonal *s = _perarr[indexPath.row];
    cell.myImage2.image = [UIImage imageNamed:[s picStr]];
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yOffset = scrollView.contentOffset.y;
    NSLog(@"yOffset===%f",yOffset);
    CGFloat xOffset = (yOffset + kImageOriginHight)/2;
    if (yOffset < -kImageOriginHight) {
        CGRect f = self.image2.frame;
        f.origin.y = yOffset - kTempHeight;
        f.size.height =  -yOffset + kTempHeight;
        f.origin.x = xOffset;
        f.size.width = self.view.frame.size.width + fabsf(xOffset)*2;
        self.image2.frame = f;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    // 取消选中阴影效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 3) {
        JHEliminateViewController *elimc = [[JHEliminateViewController alloc] init];
        [self.navigationController pushViewController:elimc animated:YES];
    }else if (indexPath.row == 4) {
        UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"开发团队:ClearLove" message:@"团队成员:\n康俊涵\n孙家庆" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
        [self.view addSubview:alertView1];
        [alertView1 show];

    }else if (indexPath.row == 2) {
        [self clear];
    }else
        if (indexPath.row == 1) {
            JHPlayBackViewController *pbackC = [[JHPlayBackViewController alloc] init];
            [self presentViewController:pbackC animated:YES completion:^{
                
            }];

        }else
            if (indexPath.row == 0) {
                JHCollectViewController *col = [[JHCollectViewController alloc] init];
                [self.navigationController pushViewController:col animated:YES];
            }
    
}
- (void)zhuangtai
{
    
    self.b =  [ShareSDK hasAuthorizedWithType:ShareTypeSinaWeibo];
    // NSLog(@"%d", _b);
    
}
- (void)zhuxiao
{
    
    [self zhuangtai];
    if (_b == 1) {
        [ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
        [_button1 setTitle:@"登录" forState:UIControlStateNormal];
       _image1.image = [UIImage imageNamed:@"788d0f720f450544ae54f910e0e1f3b5-2"];
        _b = NO;
    }else {

        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您还没有登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.view addSubview:al];
        [al show];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
